fn main() {
    let a = 3;
    if a < 5 {
        println!("is true");
    } else {
        println!("is false");
    }

    if a % 2 == 2 {
        println!("is even");
    } else {
        println!("is odd");
    }

    let condition = true;
    let number = if condition { 3 } else { 4};
    println!("number is:{number}");

    let mut counter = 0;
    let result = loop {
        counter += 1;
        if counter == 10 {
            break counter * 3;
        }
    };
    println!("result is:{}", result);

    counter = 0;
    'counting_up: loop {
        println!("counter is:{}", counter);
        let mut remaining = 10;
        loop {
            println!("remaining is:{}", remaining);
            if remaining == 9 {
                break;
            }
            if counter == 2 {
                break 'counting_up;
            }
            remaining -= 1;
        }
        counter += 1;
    }

    let a = [1, 3, 5, 6, 7, 8];
    let mut i = 0;
    while i < a.len() {
        println!("the value is:{}", a[i]);
        i += 1;
    }

    for ele in a {
        println!("the value is:{}", ele);
    }

    for i in (1..4).rev() {
        println!("the value is:{}", i);
    }

    let fib_result = fib(5);
    println!("fib result is:{}", fib_result);

    let mut s = String::from("hello");
    let s_len = calc_len(&mut s);
    s.push_str(", ok");
    println!("s_len is:{}, s:{}", s_len, s);

    // println!("{}", dangle());
    println!("{}", no_dangle());
    let s = String::from("hello world!");
    let first = first_word(&s);
    println!("first word is:{}", first);

    let a = [1, 2, 3];
    println!("a is:{:?}", &a[..2]);
}

fn fib(n: i32) -> i32 {
    if n < 2 {
        return 1;
    }
    fib(n - 1) + fib(n - 2)
}

fn calc_len(s: &mut String) -> usize {
    s.push_str(", world");
    s.len()
}

// 引用，不会转移所有权，所以在出作用域后drop掉了，会出现悬垂指针
// fn dangle() -> &String {
//     let s = String::from("dangle");
//     &s
// }

// 所有权被转移，不会出现悬垂指针
fn no_dangle() -> String {
    let s = String::from("no dangle");
    s
}

fn first_word(s: &str) -> &str {
    let bytes = s.as_bytes();
    let mut i = 0;
    for item in bytes {
        println!("{}", item);
        if *item == b' ' {
            return &s[..i];
        }
        i += 1;
    }
    &s[..]
}